package com.woniuxy.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.admin.dto.AdminAccountPageDto;
import com.woniuxy.admin.dto.PawnShopDto;
import com.woniuxy.admin.entity.PawnShop;
import com.woniuxy.admin.form.InsertPawnShopForm;
import com.woniuxy.admin.form.InsertShopAssistantForm;
import com.woniuxy.admin.form.UpdatePawnShopForm;
import com.woniuxy.admin.service.PawnShopService;
import com.woniuxy.core.annotation.Permission;
import com.woniuxy.core.exception.ParamException;
import com.woniuxy.core.util.Result;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ...
 * @since 2024年05月22日
 */
@RestController
@RequestMapping("/pawnShop")
@Api(tags="门店管理")
public class PawnShopController {

    @Resource
    private PawnShopService pawnShopService;

    /**
     * 分页查询所有门店
     * @param pageNum
     * @param pageSize
     * @param condition
     * @return
     */
    @GetMapping("/loadAllPawnShop")
    @ApiOperation("分页查询所有门店")
    @ApiResponses({
         @ApiResponse(code = 200,message = "OK",response = PawnShopDto.class)
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum",value = "当前页"),
            @ApiImplicitParam(name="pageSize",value = "每页几条"),
            @ApiImplicitParam(name="condition",value = "搜索条件")
    })
    public Result loadAllPawnShop(Integer pageNum,Integer pageSize,String condition){
    Page<PawnShopDto> pages=pawnShopService.loadAllPawnShop(pageNum,pageSize,condition);
    return Result.success("查询所有门店成功",pages);
    }

    /**
     * 新增门店
     * @param form 门店基本信息
     * @return
     */
    @PostMapping("/insertPawnShop")
    @ApiOperation("新增门店")
    @Permission(roles ={"超级管理员","管理员"})
    public Result insertPawnShop(@Validated @RequestBody InsertPawnShopForm form, BindingResult br){
        if(br.hasErrors()) throw new ParamException(10001,"参数有误");

        pawnShopService.insertPawnShop(form);
    return Result.success("新增门店成功",null);
    }

    /**
     * 修改门店信息
     * @param form 门店基本信息
     * @return
     */
    @PostMapping("/updatePawnShop")
    @ApiOperation("修改门店信息")
    @Permission(roles = {"超级管理员","管理员"})
    public Result updatePawnShop(@Validated @RequestBody  UpdatePawnShopForm form,BindingResult br){
        if(br.hasErrors()) throw new ParamException(10001,"参数有误");
        pawnShopService.updatePawnShop(form);
        return Result.success("修改门店成功",null);
    }

    /**
     * 修改门店状态
     * @param shopId 门店Id
     * @param state 门店状态
     * @return
     */
    @GetMapping("/updatePawnShopState")
    @ApiOperation("修改门店状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name="shopId",value = "门店ID"),
            @ApiImplicitParam(name="state",value = "状态")
    })
    @Permission(roles = {"超级管理员","管理员"})
    public Result updatePawnShopState(Integer shopId,Integer state){
    pawnShopService.updatePawnShopState(shopId,state);
     return Result.success("更新门店状态成功",null);
    }

    /**
     * 根据Id查询门店
     * @param shopId 门店id
     * @return
     */
    @GetMapping("/selectPawnShopById")
    @ApiOperation("根据Id查询门店")
   @ApiImplicitParam(name = "shopId",value ="门店ID" )
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = PawnShopDto.class)
    })
    public Result selectPawnShopById(Integer shopId){
        PawnShopDto dto=pawnShopService.selectPawnShopById(shopId);
        return Result.success("根据Id查询门店成功",dto);
    }

    /**
     * 添加门店店员
     *
     * @return
     */
    @PostMapping("/insertShopAssistant")
    @ApiOperation("添加门店店员")
    @ApiImplicitParams({
       @ApiImplicitParam(name = "shopId",value ="门店ID" ),
       @ApiImplicitParam(name = "accounts",value ="店员账号集合" )
    })
    @Permission(roles = {"超级管理员","管理员"})
    public Result insertShopAssistant(@RequestBody InsertShopAssistantForm form){
        pawnShopService.insertShopAssistant(form);
        return Result.success("新增店员成功",null);
    }

    /**
     * 通过账号id查门店
     * @param accountId 账号ID
     * @return 门店对象
     */
    @GetMapping("/findShopByAccountId")
    @ApiOperation("通过账号id查门店")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountId",value ="账号ID" )
    })
    public Result findShopByAccountId(Integer accountId) throws Exception {
        PawnShop shop = pawnShopService.findShopByAccountId(accountId);
        return Result.success("通过账号id查门店成功",shop);
    }

    /**
     * 根据门店Id查询店员信息
     * @param shopId
     * @return
     */
    @GetMapping("/findShopAssistantByShopId")
    @ApiOperation("根据门店Id查询店员信息")
    public Result findShopAssistantByShopId(Integer shopId,Integer is){
     List<AdminAccountPageDto>dtos=pawnShopService.findShopAssistantByShopId(shopId,is);
    return Result.success("查询店员信息成功",dtos);
    }

    /**
     * 查询所有未绑定店员信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findAllShopAssistant")
    @ApiOperation("查询所有未绑定店员信息")
    @Permission(roles = {"超级管理员","管理员"})
    public Result findAllShopAssistant(Integer pageNum,Integer pageSize){
    Page<AdminAccountPageDto> pages=pawnShopService.findAllShopAssistant(pageNum,pageSize);
      return  Result.success("查询店员成功",pages);
    }



}

